package programsettings;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class ProgramSettings {

	private static ProgramSettings SINGLETON = new ProgramSettings();
	
	private boolean hideWindowDecorations = false;
	private boolean alwaysOnTop = true;
	private String settingsFile = "settings.txt";
	private FileReader settingsReader;
	private FileWriter settingsWriter;
	
	public static ProgramSettings getInstance() {
		
		return SINGLETON;
	}
	
	private ProgramSettings() {
		
		readSettings();
	}
	
	public boolean getShowWindowDecorations() {
		
		return hideWindowDecorations;
	}
	
	public void setShowWindowDecorations(boolean showWindowDecorations) {
		
		this.hideWindowDecorations = showWindowDecorations;
		saveSettings();
	}
	
	public boolean isAlwaysOnTop() {
		
		return alwaysOnTop;
	}

	public void setAlwaysOnTop(boolean alwaysOnTop) {
		
		this.alwaysOnTop = alwaysOnTop;
		saveSettings();
	}

	private void saveSettings()  {
		
		try {
			settingsWriter = new FileWriter(new File(settingsFile));
			BufferedWriter buff = new BufferedWriter(settingsWriter);
			
			buff.write(""+hideWindowDecorations + "\n");
			buff.write("" + alwaysOnTop + "\n");
			
			buff.close();
			settingsReader.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void readSettings() {
		
		String hideWindowDecorationsLine = "";
		String alwaysOnTopLine = "";
		try {
			settingsReader = new FileReader(new File(settingsFile));
			BufferedReader buff = new BufferedReader(settingsReader);
			
			hideWindowDecorationsLine = buff.readLine();
			alwaysOnTopLine = buff.readLine();
			
			buff.close();
			settingsReader.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("Hide win dec: " + hideWindowDecorationsLine + " Always on top: " + alwaysOnTopLine);
		
		if(hideWindowDecorationsLine.equals("true")) {
			hideWindowDecorations = true;
		} else if(hideWindowDecorationsLine.equals("false")) {
			hideWindowDecorations = false;
		}
		
		if(alwaysOnTopLine.equals("true")) {
			alwaysOnTop = true;
		} else if(alwaysOnTopLine.equals("false")) {
			alwaysOnTop = false;
		}
		
		
	}
}
